﻿#pragma once

#include "Common.h"
#include "ConcurrencyMemoryPool.h"
#include "PageMap.h"

// 单例模式
class PageCache
{
public:
	static PageCache* GetInstance()
	{
		return &_sInst;
	}

	std::mutex& GetMutex()
	{
		return _pMutex;
	}

	Span* NewSpan(PAGE_ID pageNum);
	Span* MapObjectToSpan(void* obj);
	void ReleaseSpanToPageCache(Span* span);

private:
	PageCache() {}
	PageCache(const PageCache&) = delete;
	PageCache& operator=(const PageCache&) = delete;

private:
	static PageCache _sInst;
	SpanList _spanLists[NPAGES];
	ObjectPool<Span> _spanPool;

	std::mutex _pMutex;
	// 一层基数树
	//TCMalloc_PageMap1<32 - PAGE_SHIFT> _idSpanMap;
	// 两层基数树
	TCMalloc_PageMap2<32 - PAGE_SHIFT> _idSpanMap;
	//std::map<PAGE_ID, Span*> _idSpanMap;
};